﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using My.DataAccess.Core;
using NCLCustomerManagement.DataAccess.CustomerRepository;
using NCLCustomerManagement.DataTransferObjects.Customer;
using NCLCustomerManagement.DataTransferObjects.User;

namespace NCLCustomerManagement.DataAccess.User
{
    public class UserRequestRepository : BaseRepository<UserRequestDTO, UserRequest>, IUserRequestRepository
    {
        private const int INITIAL_STATUS = 0;
        private const int APPROVED_STATUS = 1;
        private const int REJECTED_STATUS = 2;



        public UserRequestRepository()
        {

        }

        public void Approve(int id)
        {
            var request = GetRequest(id);
            request.Status = APPROVED_STATUS;
            UnitOfWork.SubmitChanges();
        }

        public void Reject(int id)
        {
            var request = GetRequest(id);
            request.Status = REJECTED_STATUS;
            UnitOfWork.SubmitChanges();
        }

        public void Delete(int id)
        {
            var request = GetRequest(id);
            UnitOfWork.DeleteOnSubmit<UserRequest>(request);
            UnitOfWork.SubmitChanges();
        }

        public IEnumerable<UserRequestDTO> GetSentRequests(string user)
        {
            var requests = UnitOfWork.Table<UserRequest>()
               .Where(e => e.Requester == user)
               .Select(e => ConvertEntityToDTO(e))
               .AsEnumerable();
            return requests;
        }

        public IEnumerable<UserRequestDTO> GetReceivedRequests(string user)
        {
            var requests = UnitOfWork.Table<UserRequest>()
                .Where(e => e.RequestTo == user)
                .Select(e => ConvertEntityToDTO(e))
                .AsEnumerable();
            return requests;
        }

        private UserRequest GetRequest(int id)
        {
            return UnitOfWork.Table<UserRequest>().SingleOrDefault(e => e.Id == id);
        }

        protected override UserRequestDTO ConvertEntityToDTO(UserRequest entity)
        {
            var dto = new UserRequestDTO()
            {
                Id = entity.Id,
                Status = (RequestStatus)entity.Status,
                Requester = entity.Requester,
                RequestTo = entity.RequestTo,
                Viewed = entity.Viewed,
                RequestedDate = entity.RequestedDate.Value,
                ApproveRejectDate = entity.ApproveRejectDate
            };

            dto.CurrentCustomer = AutoMapperBuilder.MapSourceToDestination<Customer, CustomerDTO>(entity.Customer);
            dto.CurrentCustomer.Id = entity.Customer.Id;
            dto.CurrentCustomer.CustomerRegion = (Region)entity.Customer.Region;
            var cates = from c in entity.Customer.Customer_ProductCategories
                        select c.ProductCategory;
            dto.CurrentCustomer.Products = cates
                .Select(e => AutoMapperBuilder.MapSourceToDestination<ProductCategory, ProductCategoryDTO>(e))
                .ToList();

            dto.NewCustomer = AutoMapperBuilder.MapSourceToDestination<UserRequest, CustomerDTO>(entity);
            dto.NewCustomer.Id = entity.Customer.Id;
            dto.NewCustomer.CustomerRegion = (Region)entity.Region;

            if (!string.IsNullOrEmpty(entity.AddedProductCategories))
            {
                dto.NewCustomer.Products =
                    entity.AddedProductCategories.Split(',')
                    .Select(e => new ProductCategoryDTO() { Id = int.Parse(e.Trim()) })
                    .ToList();
            }
            else dto.NewCustomer.Products = new List<ProductCategoryDTO>();
            return dto;
        }


        public IEnumerable<UserRequestDTO> GetAllRequests()
        {
            var all = UnitOfWork.Table<UserRequest>()
                .Select(e => ConvertEntityToDTO(e))
                .AsEnumerable();
            return all;
        }




        public void UpdateRequestAsViewed(int id)
        {
            var rq = GetRequest(id);
            rq.Viewed = true;
            UnitOfWork.SubmitChanges();
        }


        UserRequestDTO IUserRequestRepository.GetRequest(int id)
        {
            var entity = UnitOfWork.Table<UserRequest>().SingleOrDefault(e => e.Id == id);
            return ConvertEntityToDTO(entity);
        }
    }


}
